package edu.nf.controller.user;

import edu.nf.controller.BaseController;
import edu.nf.hotel.roominfo.RoomInfoDao;
import edu.nf.hotel.roominfodao.impl.RoomInfoDaoImpl;
import edu.nf.hotel.user.LoginService;
import edu.nf.hotel.user.User;
import edu.nf.hotel.user.impl.LoginServiceImpl;
import edu.nf.hotel.userdao.UserDao;
import edu.nf.hotel.userdao.impl.UserDaoImpl;
import edu.nf.hotel.vo.ResultVO;
import org.nf.web.annotation.RequestMapping;
import org.nf.web.annotation.RequestParam;
import org.nf.web.servlet.View;
import org.nf.web.servlet.view.JsonView;

import javax.servlet.http.HttpSession;

/**
 * @Date 2023-06-14
 * @Author 陈
 */
public class LoginController extends BaseController {

    /**
     * 获取用户名和密码登录的功能
     * @param username
     * @param password
     * @param session
     * @return
     * 实现登录系统
     */
    @RequestMapping("/user/login")
    public View Login(@RequestParam("username") String username,@RequestParam("password") String password, HttpSession session) {

        LoginService loginService = new LoginServiceImpl();

        User user = loginService.login(username,password);

        session.setAttribute("user",user);

        return new JsonView(success("user/user.jsp","登录成功"));

    }


    /***
     * 调用响应类
     * @return
     */
    private static ResultVO<Object> resultVO = null ;

    /**
     * 添加会员
     * @param name
     * @param password
     * @param sex
     * @param phone
     * @param email
     * @param address
     * @return
     */
    @RequestMapping("/user/getAddUser.do")
    public View AddUser(@RequestParam("userName") String name,
                           @RequestParam("password")String password,
                           @RequestParam("sex")String sex,
                           @RequestParam("phone")String phone,
                           @RequestParam("email")String email,
                           @RequestParam("address")String address){
        LoginService service = new LoginServiceImpl() ;
        resultVO = new ResultVO<>(200,"成功",service.add(name, password, sex, phone, email, address));
        return new JsonView(resultVO);
    };

    /**
     * 查询会员所有信息
     * @return
     */
    @RequestMapping("/user/getUser")
    public View getUser(){
        UserDao dao = new UserDaoImpl();
        ResultVO<Object> resultVO1 = new ResultVO<>(0,"成功",dao.selectUser());
        return new JsonView(resultVO1);
    }

    /**
     * 修改会员信息
     */
    @RequestMapping("/user/update")
    public View updateUser(@RequestParam("uid") int id,
                           @RequestParam("username") String userName,
                           @RequestParam("passwords")String password,
                           @RequestParam("uSex")String sex,
                           @RequestParam("uPhone")String phone,
                           @RequestParam("uEmail")String email,
                           @RequestParam("uAddress")String address){
        LoginService service = new LoginServiceImpl();
        ResultVO<Object> vo = new ResultVO<>(200,"成功",service.update(id,userName,password,sex,phone,email,address));
        return new JsonView(vo);
    }


}
